//
//  HubuFourthViewController.m
//  iHubu2
//
//  Created by hanshui on 12-10-31.
//  Copyright (c) 2012年 hanshui. All rights reserved.
//

#import "HubuFourthViewController.h"
#import "BookItem.h"
#import "DBUtil.h"
#import "BookConstants.h"
#import "TableCell3.h"

@interface HubuFourthViewController ()

@end

@implementation HubuFourthViewController
@synthesize booksTableView;
@synthesize hot1Button, new1Button;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self hotAndNewClick:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)hotAndNewClick:(id)sender {
    int hotAndNew = 1;
    if(sender != nil) {
        hotAndNew = ((UIButton *)sender).tag;
    }
    
    if(hotAndNew == 1) {
        [hot1Button setBackgroundImage:[UIImage imageNamed:@"ihubei_category_title_bg.png"] forState:UIControlStateNormal];
        [new1Button setBackgroundImage:nil forState:UIControlStateNormal];
    } else {
        [new1Button setBackgroundImage:[UIImage imageNamed:@"ihubei_category_title_bg.png"] forState:UIControlStateNormal];
        [hot1Button setBackgroundImage:nil forState:UIControlStateNormal];
    }
    
    if(bookData == nil) {
        bookData = [[NSMutableArray alloc] initWithCapacity:5];
    } else {
        [bookData removeAllObjects];
    }
    
    DBUtil *db = [[DBUtil alloc] init];
    [db openDB];
    if(hotAndNew == 1) {
        [db getTopData:bookData page:@"1" keyword:@""];
    } else {
        [db getTopNewData:bookData page:@"1" keyword:@""];
    }
    [db closeDB];
    [db release];
    
    [self.booksTableView reloadData];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [bookData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /*static NSString *CellTableIdentifier = @"CellTableIdentifier";
     
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
     CellTableIdentifier];
     if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
     reuseIdentifier: CellTableIdentifier] autorelease];
     }
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.textLabel.text = @"Text";
     */
    /*
     CategoryCount *categoryCount = [categoriesData objectAtIndex:[indexPath row]];
     cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)",
     categoryCount.categoryName,
     categoryCount.categoryCount];
     cell.textLabel.textColor = [UIColor whiteColor];*/
    //cell.imageView.image = [UIImage imageNamed:@"gz_dot_2.png"];
    static NSString *CellTableIdentifier = @"CellTableIdentifier2";
    TableCell3 *cell = (TableCell3 *)[tableView dequeueReusableCellWithIdentifier:
                                      CellTableIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TableCell3" owner:self options:nil] objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BookItem *bookItem = [bookData objectAtIndex:[indexPath row]];
    [cell initExtend:bookItem];
    [[cell buyButton] setTag:([[bookItem pkId] intValue] + buyBtnTagBegin)];
    [cell setTag:([[bookItem pkId] intValue] + buyBtnParentTagBegin)];
    return cell;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 86;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}
@end
